package com.northwest_man.refactor_tool;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


// 删除暂未能分离的类
public class DeleteImportedJavaFile {

    // import\s+(com\.interfocusllc\.patpat\.[^;]+);
    private static final String regex = "import\\s+(com\\.interfocusllc\\.patpat\\.[^;]+);";


    private static final String module_name = "lib_pojo";
    private static final String javaFileDir = "/Users/yong.hu/AndroidStudioProjects/patpat/" + module_name + "/src/main/java";

    private static final boolean delete = true;
    private static final String from_module_root_dir = "/Users/yong.hu/AndroidStudioProjects/patpat/Patpat";
    private static final String to_module_root_dir = "/Users/yong.hu/AndroidStudioProjects/patpat/" + module_name;
    private static final String aim_dir = "/src/main/java";

    private static final String from_module_aim_dir = from_module_root_dir + aim_dir;
    private static final String to_module_aim_dir = to_module_root_dir + aim_dir;

    public static void main() {
        // 需要删除的文件
        final List<String> fileNameShouldDeleteList = new ArrayList<>(Arrays.asList(new String[]{

                "AddressInfoHint",
                "MsgListResp"
        }));

        final File folder = new File(to_module_aim_dir);

        // 遍历到的每个文件执行什么操作
        final Handler handler = new Handler<Boolean>() {
            @Override
            public Boolean doHandle(File curFile, List<String> allFileAbsolutePath) {

                for (final String fileNameWithoutSuffix : fileNameShouldDeleteList) {

                    final String curFileNameWithoutSuffix = FileUtil.getFileNameWithoutSuffix(curFile);
                    if (fileNameWithoutSuffix.equals(curFileNameWithoutSuffix)) return false;
                }
                for (final String fileNameWithoutSuffix : fileNameShouldDeleteList) {
                    final StringBuilder fileContent = FileUtil.getFileContent(curFile);
                    if (fileContent == null) {
                        System.out.println("文件读取失败：" + curFile.getAbsolutePath());
                        continue;
                    }
                    final Pattern pattern = Pattern.compile("import[^;]+?" + fileNameWithoutSuffix + ";");
                    final Matcher matcher = pattern.matcher(fileContent);
                    if (matcher.find()) {
                        final String curFileNameWithoutSuffix = FileUtil.getFileNameWithoutSuffix(curFile);
                        if (curFileNameWithoutSuffix != null) {
                            fileNameShouldDeleteList.add(curFileNameWithoutSuffix);
                        }
                        return true;
                    }


                }
                return false;
            }
        };
        while (count(folder, null, handler)) {

        }
        for (final String fileName : fileNameShouldDeleteList) {
            System.out.println(fileName);
            count2(folder, null, new Handler<Boolean>() {
                @Override
                public Boolean doHandle(File curFile, List<String> allFileAbsolutePath) {
                    final String fileName2 = FileUtil.getFileNameWithoutSuffix(curFile);
                    if (fileName.equals(fileName2)) {
                        curFile.delete();
                        return true;
                    }
                    return false;
                }
            });
        }
    }

    /**
     * 此次递归是否找到满足条件的类
     *
     * @return
     */
    private static boolean count(final File folder, final List<String> allFileAbsolutePath, final Handler<Boolean> handler) {


        if (folder.isDirectory()) {
            final File[] files = folder.listFiles();
            boolean found = false;
            for (final File file : files) {
                if (file.isDirectory()) {
                    if (count(file, allFileAbsolutePath, handler)) {
                        found = true;
                    }
                } else {

                    if (handler.doHandle(file, allFileAbsolutePath)) {
                        found = true;
                    }

                }
            }
            return found;
        } else {
            return handler.doHandle(folder, allFileAbsolutePath);

        }
    }

    private static boolean count2(final File folder, final List<String> allFileAbsolutePath, final Handler<Boolean> handler) {


        if (folder.isDirectory()) {
            final File[] files = folder.listFiles();
            for (final File file : files) {
                if (file.isDirectory()) {
                    if (count2(file, allFileAbsolutePath, handler)) {
                        return true;
                    }
                } else {

                    if (handler.doHandle(file, allFileAbsolutePath)) {
                        return true;

                    }

                }
            }

        } else {
            if (handler.doHandle(folder, allFileAbsolutePath))
                return true;


        }
        return false;
    }


}
